<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="optType=='save'||optType=='edit'"
          v-hasPermi="['pro:outsourcingplanline:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="lineList" @selection-change="handleSelectionChange">
      <el-table-column label="产品编号" align="center" prop="productCode" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleInfoView(scope.row)">{{ scope.row.productCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />

      <el-table-column label="需求数量" align="center" prop="productQuantity" >
        <template slot-scope="scope">
          <el-input
            type="text"
            size="mini"
            v-model="scope.row.productQuantity"
            v-if="productEdit"
          >
          </el-input>
          <label   v-else>{{scope.row.productQuantity}}</label>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="optType=='save'||optType=='edit'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:outsourcingplan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSave(scope.row)"
            v-hasPermi="['pro:outsourcingplan:edit']"
          >保存</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pro:outsourcingplan:edit']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <BomSelectSingle ref="bomlist" :planning="{'productId':bomProductId}" @onSelected="onBomSelected" ></BomSelectSingle>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外协订单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请选择产品">
                <el-button
                  slot="append"
                  @click="handleSelectProduct"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
              <ItemSelect ref="itemSelect" item-or-product="PRODUCT" @onSelected="onItemSelected"></ItemSelect>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="产品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" readonly  />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="需求数量" prop="productQuantity">
              <el-input v-model="form.productQuantity" type="number" placeholder="请输入需求数量" @blur="getTotalPrice" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价(元)" prop="price">
              <el-input v-model="form.price" type="number" placeholder="请输入单价" @blur="getTotalPrice" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品总价(元)" prop="totalPrice">
              <el-input v-model="form.totalPrice" placeholder="请输入产品总价" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLine, getLine, delLine, addLine, updateLine } from "@/api/mes/pro/outsourcingplan";
import ProductSelect from "@/components/productSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import BomSelectSingle from "@/components/mdBomSelect/single.vue";
import Template from "@/views/print/printtemplate/list.vue";

export default {
  name: "planLine",
  components: {Template, BomSelectSingle, ItemSelect, ProductSelect},
  props: {
    plan: {
      type: Object,
      default: () => {planId:null    }
    },
    optType:{
      type: String,
      default: 'edit'
    },
  },
  data() {
    return {
      bomRow:null,
      bomProductId:null,
      productEdit:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协订单明细表格数据
      lineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,        productName: null,      },
      // 表单参数
      form: {  totalPrice: 0,        productQuantity: 0,        price: 0,  },
      // 表单校验
      rules: {
        planId: [
          { required: true, message: "外协订单ID不能为空", trigger: "blur" }
        ],        productId: [
          { required: true, message: "产品ID不能为空", trigger: "blur" }
        ],        productCode: [
          { required: true, message: "产品编号不能为空", trigger: "blur" }
        ],        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],        productQuantity: [
          { required: true, message: "需求数量不能为空", trigger: "blur" }
        ],      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSave(row){
      this.productEdit=false;
    },
    handleSelectBom(row){
      this.bomRow=row;
      this.bomProductId=row.productId;
      this.$refs.bomlist.showFlag=true;
    },
    onBomSelected(row){
      this.bomRow.bomId=row.bomId;
      this.bomRow.bomName=row.bomName;
    },
    getTotalPrice(){
      if(this.form.price!=null&&this.form.price!=''&&this.form.productQuantity!=null&&this.form.productQuantity!=''){
        this.form.totalPrice =  this.form.productQuantity*this.form.price;
      }
    },
    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    //物料选择弹出框
    onItemSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.productId = obj.itemId;
        this.form.productCode = obj.itemCode;
        this.form.productName = obj.itemName;
        this.form.specification = obj.specification;
        this.form.unitOfMeasure = obj.unitOfMeasure;
        this.form.bomId=obj.bomId;
        this.form.bomCode = obj.bomCode;
        this.form.bomName = obj.bomName;
        //this.form.itemOrProduct = obj.itemOrProduct;
        /*if (this.form.planId != null) {
          this.$refs.bomlist.planningbomList = [];
          this.$refs.bomlist.bomCode = undefined;
          this.$refs.bomlist.bomName = undefined;
          this.$refs.bomlist.bomId = undefined;
        }*/
      }
    },
    /** 查询外协订单明细列表 */
    getList() {
      this.loading = true;
      listLine(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        planId: null,        productId: null,        productCode: null,        productName: null,
        totalPrice: 0,        productQuantity: 0,        price: 0,            createBy: null,        createTime: null,   bomId:null,bomCode:null,bomName:null,
        updateBy: null,        updateTime: null,        tenantId: null,        specification: null,        unitOfMeasure: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加外协订单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外协订单明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$emit('callParentMethod',this.form.planId);
            });
          } else {
            this.form.planId = this.plan.planId;
            addLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.$emit('callParentMethod',this.form.planId);
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除外协订单明细编号为"' + ids + '"的数据项？').then(function() {
        return delLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/line/export', {
        ...this.queryParams
      }, `line_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
